import 'package:flutter/services.dart';
import 'package:jpush_flutter/jpush_flutter.dart';

class JPushMan {
  static final JPushMan instance = JPushMan._inverval();
  factory JPushMan() => instance;
  JPushMan._inverval() {
    init();
  }
  init() {}

  final JPush jpush = JPush();
  void initJpush() {
    try {
      jpush.addEventHandler(
          onReceiveNotification: (Map<String, dynamic> message) async {
        print("flutter onReceiveNotification: $message");
        responseNoti(message);
      }, onOpenNotification: (Map<String, dynamic> message) async {
        print("flutter onOpenNotification: $message");
        responseNoti(message);
      }, onReceiveMessage: (Map<String, dynamic> message) async {
        print("flutter onReceiveMessage: $message");
        responseNoti(message);
      }, onReceiveNotificationAuthorization:
              (Map<String, dynamic> message) async {
        print("flutter onReceiveNotificationAuthorization: $message");
        responseNoti(message);
      });
    } on PlatformException {}

    jpush.setup(
      appKey: "be36f3d6dc5a137dc3d7e35f", //你自己应用的 AppKey
      channel: "lewm-default",
      production: false,
      debug: true,
    );

    jpush.applyPushAuthority(
        new NotificationSettingsIOS(sound: true, alert: true, badge: true));
    jpush.getRegistrationID().then((rid) {
      print("flutter get registration id : $rid");
    });
  }

  responseNoti(Map<String, dynamic> message) {
    StringCodec title = message['title'];
    String alert = message['alert'];
    Map extras = message['extras'];
  }

  addLocalNoti(Map<String, dynamic> message) {
    var localNotification = LocalNotification(
        id: 234,
        title: message['title'],
        buildId: 1,
        content: 'fdas',
        fireTime: DateTime.now(),
        subtitle: 'fasf',
        badge: 5,
        extra: {"fa": "0"});
    jpush.sendLocalNotification(localNotification).then((res) {});
  }
}
